package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.atguigu.gulimall.product.vo.CategoryGroupAttrRelationAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author wangliang
 * @email wangliang@gmail.com
 * @date 2023-11-07 22:54:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @GetMapping("/{catelogId}/withattr")
    public R getCategoryGroupAttrRelationAttr(@PathVariable Long catelogId){
      List<CategoryGroupAttrRelationAttrVo> list   = attrGroupService.getCategoryGroupAttrRelationAttr(catelogId);
      return R.ok().put("data",list);
    }



    @RequestMapping(value = "/{attrgroupId}/noattr/relation",method = RequestMethod.GET)
    public R getNoGroupArrRelation(@RequestParam Map<String,Object> params,@PathVariable Long attrgroupId){
        PageUtils page = attrService.getNoGroupArrRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }


//    /**
//     * 获取与属性分组关联的属性信息
//     * @param attrGroupId
//     * @return
//     */
//    @GetMapping("/{attrGroupId}/attr/relation")
//    public R getAllRelationAttr(@PathVariable Long attrGroupId){
//        List<AttrEntity> list = attrGroupService.getAllRelationAttr(attrGroupId);
//        return R.ok().put("data",list);
//    }

    /**
     * 获取当前分组下关联的属性信息
     */
    @RequestMapping(value = "/{attrGroupId}/attr/relation",method = RequestMethod.GET)
    public R getGroupAttrRelation(@PathVariable Long attrGroupId){
        List<AttrEntity> list = attrService.getGroupAttrRelation(attrGroupId);
        return R.ok().put("data",list);
    }

    /**
     * 移除当前分组下关联的属性信息
     * @return
     */
    @RequestMapping(value = "/attr/relation/delete",method = RequestMethod.POST)
    public R deleteGroupAttrRelation(@RequestBody List<AttrGroupVo> attrGroupVos){
         attrService.deleteGroupAttrRelation(attrGroupVos);
         return R.ok();
    }

    /**
     * 根据指定的分类id查询对应属性分组信息
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        // 查找当前分类id的父路径
        Long[] catelogPath = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
//    api/product/attrgroup/attr/relation

    /**
     * 新增关联关系
     *
     */
    @RequestMapping(value = "/attr/relation",method = RequestMethod.POST)
    public R addRelationGroupAttr(@RequestBody List<AttrGroupVo> list){
        attrAttrgroupRelationService.addRelationGroupAttr(list);
        return R.ok();
    }

//    /**
//     * 删除属性与分组的关联关系
//     */
//    @PostMapping("/attr/relation/delete")
//    public R deleteAttrGroupRelationAttr(@RequestBody List<AttrGroupVo> list){
//            attrGroupService.deleteAttrGroupRelationAttr(list);
//            return R.ok();
//    }
}
